﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib;
using GestionePolveriLib.BL;
using GestionePolveriLib.Facades;
using Be.Timvw.Framework.ComponentModel;

namespace GestionePolveri2
{
    public partial class AnagraficaPOS : AnagraficaPOSBase
    {
        public AnagraficaPOS()
        {
            InitializeComponent();
        }

        public override void InitializeForm()
        {
            base.InitializeForm();

            if (!this.DesignMode)
            {
                this.EnableSorting = true;
                this.splitContainer1.Panel2Collapsed = true;
                this.ShowPanelMessage = false;
                this.CanModify = false;

                this.AddCustomButton("btnReload", "Ricarica Dati");
                this.AddCustomButton("btnResetPOS", "Reset POS");

                this.CustomButtonClicked += new EventHandler<CustomEventArgs<POS>>(Anagrafica_CustomButtonClicked);
                this.ItemSelected += new EventHandler<CustomEventArgs<POS>>(AnagraficaPOS_ItemSelected);
                
                LoadPOSs(false);
            }
        }

        void Anagrafica_CustomButtonClicked(object sender, CustomEventArgs<POS> e)
        {
            if (e.ButtonName.Equals("btnReload", StringComparison.InvariantCultureIgnoreCase))
            {
                LoadPOSs(false);
            }

            if (e.ButtonName.Equals("btnResetPOS", StringComparison.InvariantCultureIgnoreCase))
            {
                MovimentiFacade mf = new MovimentiFacade();
                if (!mf.CheckMovimentiTurnoImpianto(this.Current.Turno, this.Current.Impianto, "S", this.Current.DateOfPos))
                {
                    POSTableFacade ptf = new POSTableFacade();
                    this.Current.POSValue = 0;

                    if (ptf.Modify(this.Current))
                    {
                        MessageBoxA("Valore del POS resettato !\nIl prossimo valore POS sarà 1.");

                        LoadPOSs(false);
                    }
                }
                else
                {
                    MessageBoxA("Reset Annullato. Esistono dei movimenti di scarico per la commbinazione Turno/Impianto/Data !");
                }
            }
        }

        void AnagraficaPOS_ItemSelected(object sender, CustomEventArgs<POS> e)
        {
            this.ShowButton("btnDelete", false);
        }

        private void LoadPOSs(bool selectLast)
        {
            POSTableFacade pf = new POSTableFacade();

            pf.RunAsync(() => pf.GetPOSData(), (result) =>
            {
                this.Datasource = result;
            });
        }

        protected override void OnRowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            POS m_pos = this.Current;

            POSUpdateDialog pud = new POSUpdateDialog();
            pud.PosValue = m_pos;

            if (pud.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                
            }
        }
    }

    //public class AnagraficaPOSBase : AnagraficaManagerObjectListView<POS> { }
    public class AnagraficaPOSBase : AnagraficaManager<POS> { }
}
